import config from '@/config/config.js';
import http from '@/api/http.js'
import Config from "@/config/config.js";

let deviceRequest= {

    loadDeviceTypes(devicetypeQuery, parentId, page = 1, limit = 10, callBack) {
        // 获取token
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.deviceBaseUrl + "/devicetype/list?page=" + page + "&limit=" + limit;
            if (devicetypeQuery) {
                if (devicetypeQuery.deviceTypeName) {
                    url += "&deviceTypeName=" + devicetypeQuery.deviceTypeName;
                }
                if (devicetypeQuery.isEnabled == false || devicetypeQuery.isEnabled == true) {
                    url += "&isEnabled=" + devicetypeQuery.isEnabled;
                }
                if (parentId != null) {
                    url += "&parentCode=" + parentId;
                }
            }
            http.get(url, callBack);
        } else {
            callBack(undefined);
        }
    },
    addDeviceTypeForm(deviceTypeForm, callBack) {
        let token = localStorage.getItem("token");
        if (token) {
            deviceTypeForm.parent = true;
            deviceTypeForm.parentCode = 0;
            console.log(deviceTypeForm)
            let url = config.deviceBaseUrl + "/devicetype/add";
            http.post(url, deviceTypeForm, callBack);
        } else {
            callBack(undefined);
        }
    },
    deleteDeviceType(deviceTypeCode, callBack) {
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.deviceBaseUrl + "/devicetype/delete";
            url += "?deviceTypeCode=" + deviceTypeCode;

            http.delete(url, callBack);
        } else {
            callBack(undefined);
        }
    },
    editDeviceType(deviceTypeForm, callBack) {
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.deviceBaseUrl + "/devicetype/edit";
            http.post(url, deviceTypeForm, callBack);
        } else {
            callBack(undefined);
        }
    },
    loadDeviceTypesByParentCode(parentCode, callBack) {
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.deviceBaseUrl + "/devicetype/listByParentId?parentCode=" + parentCode;
            http.get(url, res => {
                callBack(res.data);
            });
        } else {
            callBack(undefined);
        }
    },
    addSonDeviceType(deviceTypeForm, parentCode, callBack) {
        let token = localStorage.getItem("token");
        if (token) {
            // 拼接 URL，确保参数都正确编码
            const url = new URL(config.deviceBaseUrl + "/devicetype/addSon");

            // 添加查询参数
            url.searchParams.append('deviceTypeCode', deviceTypeForm.deviceTypeCode);
            url.searchParams.append('deviceTypeName', deviceTypeForm.deviceTypeName);
            url.searchParams.append('enabled', deviceTypeForm.enabled);
            url.searchParams.append('parentCode', parentCode);
            url.searchParams.append('remarks', deviceTypeForm.remarks);

            http.get(url,callBack(res=>res.data()))

        }
    },loadDeviceledger(deviceledgerQuery,page = 1, limit = 10, callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(config.deviceBaseUrl + "/deviceLedger/list?page=" + page + "&limit=" + limit);
            if(deviceledgerQuery){
                if(deviceledgerQuery.deviceCode){
                    url += "&deviceCode="+deviceledgerQuery.deviceCode;
                }
                if(deviceledgerQuery.deviceName){
                    url += "&deviceName="+ deviceledgerQuery.deviceName;
                }
            }
            http.get(url,callBack);
        }else {
            callBack(undefined);
        }

    },workshoplist(callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(config.deviceBaseUrl + "/deviceWork/list");
            http.get(url,callBack)
        }else {
            callBack(undefined);
        }
    },loadCategorys(callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(Config.deviceBaseUrl + "/deviceCategory/list")
            http.get(url,callBack)
        }else {
            callBack(undefined);
        }
    },adddeviceledgeForm(deviceledgeForm,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(Config.deviceBaseUrl + "/deviceLedger/add")
            http.post(url,deviceledgeForm,callBack)
        }else {
            callBack(undefined);
        }
    },editledgeForm(deviceledgeForm,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(Config.deviceBaseUrl + "/deviceLedger/edit")
            http.post(url,deviceledgeForm,callBack)
        }else {
            callBack(undefined);
        }
    },deleteDeviceledger(id,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(Config.deviceBaseUrl + "/deviceLedger/delete?id=" + id)
            http.delete(url,callBack)
        }else {
            callBack(undefined);
        }
    },loadDeviceMainItems(Query,page,limit,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(Config.deviceBaseUrl + "/deviceMainItem/list?page=" + page + "&limit=" + limit)
            if(Query){
                if(Query.projectCode){
                    url += "&projectCode="+Query.projectCode;
                }
                if(Query.projectName){
                    url += "&projectName="+Query.projectName;
                }
                if(Query.projectType){
                    url += "&projectType="+Query.projectType;
                }
            }
            http.get(url,callBack);
        }else {
            callBack(undefined);
        }
    },adddeviceMainItemFrom(MainItemFrom,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(Config.deviceBaseUrl + "/deviceMainItem/add")
            http.post(url,MainItemFrom,callBack);
        }else {
            callBack(undefined);
        }
    },EditMainTainForm(MainItemForm,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(Config.deviceBaseUrl + "/deviceMainItem/edit")
            http.post(url,MainItemForm,callBack);
        }else {
            callBack(undefined);
        }
    },loaddeviceMainPlans(MainItemFormQuery,page = 1,limit= 10 ,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(config.deviceBaseUrl + "/deviceMainPlan/list?page=" + page + "&limit=" + limit);
            if(MainItemFormQuery){
                if(MainItemFormQuery.planCode){
                    url += "&planCode=" +MainItemFormQuery.planCode;
                }
                if(MainItemFormQuery.planName){
                    url += "&planName=" +MainItemFormQuery.planName;
                }
                if(MainItemFormQuery.planType){
                    url += "&planType=" +MainItemFormQuery.planType;
                }
                if(MainItemFormQuery.planStatus){
                    url += "&planStatus=" +MainItemFormQuery.planStatus;
                }
            }
            http.get(url,callBack);
        }else {
            callBack(undefined);
        }
    },addDeviceMainPlanFrom(DeviceMainPlanForm,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(Config.deviceBaseUrl + "/deviceMainPlan/add");
            http.post(url,DeviceMainPlanForm,callBack);
        }else {
            callBack(undefined);
        }
    },EditMainTainPlanForm(DeviceMainPlanForm,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(Config.deviceBaseUrl + "/deviceMainPlan/edit");
            http.post(url,DeviceMainPlanForm,callBack);
        }else {
            callBack(undefined);
        }
    },loadDeviceMainPlanOfItems(planCode,page=1,limit =10,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL( Config.deviceBaseUrl + "/deviceMainPlan/listItemsByPlanId?page="+page+"&limit="+limit+"&planCode="+planCode);
            http.get(url,callBack);
        }else {
            callBack(undefined);
        }
    },loadDeviceMainPlanOfledgers(planCode,page=1,limit =10,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL( Config.deviceBaseUrl + "/deviceMainPlan/listLedgersByPlanId?page="+page+"&limit="+limit+"&planCode="+planCode);
            http.get(url,callBack);
        }else {
            callBack(undefined);
        }
    },addDeviceMainPlanOfItems(selectItems,planCode,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(Config.deviceBaseUrl + "/deviceMainPlan/addPlanOfItem");
            const projectCodes = selectItems.map(item => item.projectCode); // 假设 item 中有 projectCode 属性

            // 封装成请求体
            const requestBody = {
                projectCode: projectCodes,
                planCode: planCode
            };
            http.post(url,requestBody,callBack);

        }else {
            callBack(undefined);
        }
    },addDeviceMainPlanOfledgers(selectItem,planCode,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(Config.deviceBaseUrl+ "/deviceMainPlan/addPlanOfledger");
            const deviceCodes = selectItem.map(item => item.deviceCode);
            const requestBody={
                deviceCode: deviceCodes,
                planCode: planCode
            }
            http.post(url,requestBody,callBack);
        }else {
            callBack(undefined);
        }
    },deleteSelectionPlanOfItem(selectItem,planCode,callBack){
        let token = localStorage.getItem("token");
        const projectCodes =selectItem.map(item=>item.deviceCode);
        const requestBody={
            projectCode:projectCodes,
            planCode:planCode
        }
        if(token){
            let url = new URL(Config.deviceBaseUrl+ "/deviceMainPlan/deletePlanOfItem");
            http.post(url,requestBody,callBack);
        }else {
            callBack(undefined);
        }
    },deleteSelectionPlanOfledger(selectledgers,planCode,callBack){
        let token = localStorage.getItem("token");

        const deviceCodes =selectledgers.map(item=>item.deviceCode);
        const requestBody={
            deviceCode:deviceCodes,
            planCode:planCode
        }

        if(token){
            let url = new URL(config.deviceBaseUrl + "/deviceMainPlan/deletePlanOfledger")

            http.post(url,requestBody,callBack);
        }else {
            callBack(undefined);
        }
    },loadToolTypes(ToolTypesQuery,page=1,limit=10,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(config.deviceBaseUrl + "/toolsType/list?page="+page+"&limit="+limit);
            if(ToolTypesQuery){
                if(ToolTypesQuery.typeCode){
                    url += "&typeCode=" +ToolTypesQuery.typeCode;
                }
                if(ToolTypesQuery.typeName){
                    url += "&typeName=" +ToolTypesQuery.typeName;
                }
                if(ToolTypesQuery.maintenanceType){
                    url += "&maintenanceType=" +ToolTypesQuery.maintenanceType;
                }
            }
            http.get(url,callBack);
        }else {
            callBack(undefined);
        }
    },addtoolstypesForm(toolstyoeform,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(config.deviceBaseUrl + "/toolsType/add");
            http.post(url,toolstyoeform,callBack);
        }else {
            callBack(undefined);
        }
    },edittoolstypesForm(toolstyoeform,callBack){
    let token = localStorage.getItem("token");
    if(token){
        let url = new URL(config.deviceBaseUrl + "/toolsType/edit");
        http.post(url,toolstyoeform,callBack);
    }else {
        callBack(undefined);
    }
    },loadtoolsLedgerForm(toosLedgerForm,page=1,limit=10,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(config.deviceBaseUrl + "/toolsLedger/list?page="+page+"&limit="+limit);
            if(toosLedgerForm){
                if(toosLedgerForm.toolCode){
                    url += "&toolCode=" +toosLedgerForm.toolCode;
                }
                if(toosLedgerForm.toolName){
                    url += "&toolName=" +toosLedgerForm.toolName;
                }
                if(toosLedgerForm.toolTypeId){
                    url += "&toolTypeId=" + toosLedgerForm.toolTypeId;
                }
                if(toosLedgerForm.brand){
                    url += "&brand=" +toosLedgerForm.brand;
                }
                if(toosLedgerForm.model){
                    url += "&model=" +toosLedgerForm.model;
                }
                if(toosLedgerForm.status){
                    url += "&status=" + toosLedgerForm.status;
                }
            }
            http.get(url,callBack);
        }else {
            callBack(undefined);
        }
    },addtoolsLedgerForm(tooLedgerForm,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(config.deviceBaseUrl+"/toolsLedger/add");
            http.post(url,tooLedgerForm,callBack);
        }else {
            callBack(undefined);
        }
    },edittoolsLedgerForm(tooLedgerForm,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(config.deviceBaseUrl+"/toolsLedger/edit");
            http.post(url,tooLedgerForm,callBack);
        }else {
            callBack(undefined);
        }
    },deletetoolsLedgerForm(toolCode,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = new URL(config.deviceBaseUrl+"/toolsLedger/delete?toolCode="+toolCode);
            http.get(url,callBack);
        }else {
            callBack(undefined);
        }
    }

}
export default deviceRequest;